<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="危废名称" prop="wasteName">
                <el-input v-model="queryParams.wasteName" placeholder="请输入危废名称" clearable size="small" @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="处置公司" prop="handleCompanyKey">
                <el-select v-model="queryParams.handleCompanyKey" placeholder="请选择处置公司名称" clearable size="small">
                    <el-option v-for="dict in handleCompanyKeyOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
                </el-select>
            </el-form-item>
            <el-form-item label="转移联单" prop="transferNumber">
                <el-input v-model="queryParams.transferNumber" style="width:80%" placeholder="请输入转移联单号" />
            </el-form-item>
            <el-form-item label="出厂日期">
                <el-date-picker v-model="daterangeOutFactoryDate" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['business:outFactory:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single || isOverSevenDay" @click="handleUpdate" v-hasPermi="['business:outFactory:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple || isOverSevenDay" @click="handleDelete" v-hasPermi="['business:outFactory:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport" v-hasPermi="['business:outFactory:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="outFactoryList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="委外出厂ID" align="center" prop="outFactoryId" v-if="false" />
            <el-table-column label="危废名称" align="center" prop="wasteName" />
            <el-table-column label="处置公司" align="center" prop="handleCompanyKey" :formatter="handleCompanyKeyFormat" />
            <el-table-column label="转移联单号" align="center" prop="transferNumber" width="150" />
            <el-table-column label="出厂日期" align="center" prop="outFactoryDate" width="120"></el-table-column>
            <el-table-column label="出厂重量" align="center" prop="outWarehouseWeight" width="100" />
            <el-table-column label="毛重" align="center" prop="totalWeight" width="100" />
            <el-table-column label="净重" align="center" prop="netWeight" width="100" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" v-if="scope.row.overDay <= 7" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['business:outFactory:edit']">修改</el-button>
                    <el-button size="mini" v-if="scope.row.overDay <= 7" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['business:outFactory:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改委外出厂对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-row :gutter="15">
                    <el-col :span="14">
                        <el-form-item label="危废名称" prop="wasteId">
                            <treeselect v-model="form.tempWasteID" size="small" :options="wasteTypeOptions" :disable-branch-nodes="true" :disabled="form.outWarehouseDetailId && form.outWarehouseDetailId > 0" :show-count="true" placeholder="请选择危废" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="9">
                        <el-form-item label="转移联单" prop="transferNumber">
                            <el-input v-model="form.transferNumber" style="width:80%" placeholder="请输入转移联单号" />
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="15">
                    <el-col :span="12">
                        <el-form-item label="处置公司" prop="handleCompanyKey">
                            <el-select v-model="form.handleCompanyKey" placeholder="请选择处置公司名称">
                                <el-option v-for="dict in handleCompanyKeyOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="出厂日期" prop="outFactoryDate">
                            <el-date-picker clearable v-model="form.outFactoryDate" type="date" :picker-options="pickerOptions" value-format="yyyy-MM-dd HH:mm:ss" :disabled="form.outWarehouseDetailId && form.outWarehouseDetailId > 0" placeholder="选择出厂日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="15" v-if="!(form.outWarehouseDetailId && form.outWarehouseDetailId > 0)">
                    <el-col :span="12">
                        <el-form-item label="毛重" prop="totalWeight">
                            <el-input-number v-model="form.totalWeight" placeholder="请输入总重" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="净重" prop="netWeight">
                            <el-input-number v-model="form.netWeight" placeholder="请输入净重" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="15">
                    <el-col :span="24">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listOutFactory, getOutFactory, delOutFactory, addOutFactory, updateOutFactory, exportOutFactory } from "@/api/business/outFactory";

import { treeselectAll } from "@/api/system/waste/type";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
    name: "OutFactory",
    components: {
        Treeselect
    },
    data() {
        return {
            //按钮loading
            buttonLoading: false,
            // 遮罩层
            loading: true,
            // 导出遮罩层
            exportLoading: false,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 委外出厂表格数据
            outFactoryList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 处置公司名称字典
            handleCompanyKeyOptions: [],
            // 危废分类选择列表
            wasteTypeOptions: [],
            // 出厂日期范围
            daterangeOutFactoryDate: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                wasteId: undefined,
                wasteName: undefined,
                batchNumber: undefined,
                handleCompanyKey: undefined,
                outFactoryDate: this.parseTime(new Date()),
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                // wasteId: [
                //     { required: true, message: "危废ID不能为空", trigger: "blur" }
                // ],
                handleCompanyKey: [
                    { required: true, message: "处置公司名称不能为空", trigger: "blur" }
                ],
            },
            pickerOptions: {
                disabledDate(time) {
                    let curDate = (new Date()).getTime();
                    // 7天
                    let sevenDayMsec = 7 * 24 * 3600 * 1000;
                    let sevenDay = curDate - sevenDayMsec;
                    return time.getTime() > Date.now() || time.getTime() < sevenDay;;
                }
            },
            // 是否超过七天
            isOverSevenDay: true,
        };
    },
    created() {
        this.getList();
        this.getDicts("waste_handle_company").then(response => {
            this.handleCompanyKeyOptions = response.data;
        });
        this.getTreeselect();
    },
    methods: {
        /** 查询委外出厂列表 */
        getList() {
            this.loading = true;
            this.queryParams.params = {};
            if (null != this.daterangeOutFactoryDate && '' != this.daterangeOutFactoryDate) {
                this.queryParams.params["beginOutFactoryDate"] = this.daterangeOutFactoryDate[0];
                this.queryParams.params["endOutFactoryDate"] = this.daterangeOutFactoryDate[1];
            }
            listOutFactory(this.queryParams).then(response => {
                this.outFactoryList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        /** 查询危废分类下拉树结构 */
        getTreeselect() {
            treeselectAll().then((response) => {
                this.wasteTypeOptions = response.data;
            });
        },
        // 处置公司名称字典翻译
        handleCompanyKeyFormat(row, column) {
            return this.selectDictLabel(this.handleCompanyKeyOptions, row.handleCompanyKey);
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                outFactoryId: undefined,
                wasteId: undefined,
                handleCompanyKey: undefined,
                outWarehouseDetailId: undefined,
                outFactoryDate: this.parseTime(new Date()),
                outWarehouseWeight: undefined,
                totalWeight: undefined,
                netWeight: undefined,
                remark: undefined
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.daterangeOutFactoryDate = [];
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.outFactoryId)
            this.isOverSevenDay = selection.some(item => item.overDay > 7);
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加记录";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.loading = true;
            this.reset();
            const outFactoryId = row.outFactoryId || this.ids
            getOutFactory(outFactoryId).then(response => {
                this.loading = false;
                this.form = response.data;
                this.form.tempWasteID = 0 - this.form.wasteId;
                this.open = true;
                this.title = "修改记录";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    this.form.wasteId = 0 - this.form.tempWasteID;
                    this.buttonLoading = true;
                    if (this.form.outFactoryId != null) {
                        updateOutFactory(this.form).then(response => {
                            this.buttonLoading = false;
                            this.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addOutFactory(this.form).then(response => {
                            this.buttonLoading = false;
                            this.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const outFactoryIds = row.outFactoryId || this.ids;
            this.$confirm('是否确认删除委外出厂编号为"' + outFactoryIds + '"的数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(() => {
                this.loading = true;
                return delOutFactory(outFactoryIds);
            }).then(() => {
                this.loading = false;
                this.getList();
                this.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            const queryParams = this.queryParams;
            this.$confirm('是否确认导出所有委外出厂数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(() => {
                this.exportLoading = true;
                return exportOutFactory(queryParams);
            }).then(response => {
                this.download(response.msg);
                this.exportLoading = false;
            }).catch(() => { });
        },
        /**
         * 危废选中
         */
        // wasteInput(node, wasteID) {
        //     wasteID = 0 - wasteID;
        //     if (wasteID && wasteID > 0) {
        //         getWasteDetail(wasteID).then((response) => {
        //             const wasteObj = response.data;
        //         });
        //     }
        // },
    }
};
</script>